<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');


class publicaciones extends MY_Controller {
	/**
	 * Evita la validacion (enfocado cuando se usa ajax). Ver mas en privilegios_model
	 * @var unknown_type
	 */
	private $excepcion_privilegio = array('');

	public function _remap($method){
		$this->carabiner->css(array(
				array('bootstrap/bootstrap-simplex.css', 'screen'),
				array('bootstrap/bootstrap-responsive.css', 'screen'),
				array('bootstrap/charisma-app.css', 'screen'),
				array('bootstrap/jquery-ui-1.8.21.custom.css', 'screen'),
				array('bootstrap/fullcalendar.css', 'screen'),
				array('bootstrap/fullcalendar.print.css', 'print'),
				array('bootstrap/chosen.css', 'screen'),
				array('bootstrap/uniform.default.css', 'screen'),
				array('bootstrap/colorbox.css', 'screen'),
				array('bootstrap/jquery.cleditor.css', 'screen'),
				array('bootstrap/jquery.noty.css', 'screen'),
				array('bootstrap/noty_theme_default.css', 'screen'),
				array('bootstrap/elfinder.min.css', 'screen'),
				array('bootstrap/elfinder.theme.css', 'screen'),
				array('bootstrap/jquery.iphone.toggle.css', 'screen'),
				array('bootstrap/opa-icons.css', 'screen'),
				array('bootstrap/uploadify.css', 'screen'),

				array('base-tic.css', 'screen')
		));
		$this->carabiner->js(array(
				array('bootstrap/jquery-1.7.2.min.js'),
				array('bootstrap/jquery-ui-1.8.21.custom.min.js'),
				array('bootstrap/bootstrap-transition.js'),
				array('bootstrap/bootstrap-alert.js'),
				array('bootstrap/bootstrap-modal.js'),
				array('bootstrap/bootstrap-dropdown.js'),
				array('bootstrap/bootstrap-scrollspy.js'),
				array('bootstrap/bootstrap-tab.js'),
				array('bootstrap/bootstrap-tooltip.js'),
				array('bootstrap/bootstrap-popover.js'),
				array('bootstrap/bootstrap-button.js'),
				array('bootstrap/bootstrap-collapse.js'),
				array('bootstrap/bootstrap-carousel.js'),
				array('bootstrap/bootstrap-typeahead.js'),
				array('bootstrap/bootstrap-tour.js'),
				array('bootstrap/jquery.cookie.js'),
				array('bootstrap/fullcalendar.min.js'),
				array('bootstrap/jquery.dataTables.min.js'),
				/** chart libraries **/
				array('bootstrap/excanvas.js'),
				array('bootstrap/jquery.flot.min.js'),
				array('bootstrap/jquery.flot.pie.min.js'),
				array('bootstrap/jquery.flot.stack.js'),
				array('bootstrap/jquery.flot.resize.min.js'),

				array('bootstrap/jquery.chosen.min.js'),
				array('bootstrap/jquery.uniform.min.js'),
				array('bootstrap/jquery.colorbox.min.js'),
				array('bootstrap/jquery.cleditor.min.js'),
				array('bootstrap/jquery.noty.js'),
				array('bootstrap/jquery.elfinder.min.js'),
				array('bootstrap/jquery.raty.min.js'),
				array('bootstrap/jquery.iphone.toggle.js'),
				array('bootstrap/jquery.autogrow-textarea.js'),
				array('bootstrap/jquery.uploadify-3.1.min.js'),
				array('bootstrap/jquery.history.js'),
				array('bootstrap/charisma.js'),
		));

    if($this->session->userdata('id_usuario'))
    {
      if ($this->session->userdata['acceso'] === 'admin' || $this->session->userdata['tipo'] === 'tic')
        $this->{$method}();
      else
        redirect(base_url('free/'));
    }
    else
      redirect(base_url());
	}

	public function index(){
		$this->carabiner->css(array(
				array('libs/jquery.slider.css', 'screen'),
				array('libs/jquery.smoothDivScroll.css', 'screen'),

         array('libs/jquery.scrollbarpaper.css', 'screen')
		));
		$this->carabiner->js(array(

				array('libs/jquery.slides.min.js'),
				array('libs/jquery.mousewheel.min.js'),
				array('libs/jquery.smoothdivscroll-1.2-min.js'),

        array('libs/jquery.scrollbarpaper.min.js'),

				array('free/free.js'),
				array('tic/carrito.js'),
				array('tic/footer.js'),
        array('http://connect.facebook.net/en_US/all.js'),
        array('tic/publicaciones.js'),
		));

		$params['info_empleado'] = $this->info_empleado['info']; //info empleado
		$params['seo'] = array(
			'titulo' => 'Acceso tic - Tienda Capp'
		);

		$id  = 0;
		if ($this->input->get('pub') > 0) {
			$id = $this->input->get('pub');
		}

		//url redirect de las paginas
		$params['url_redirect'] = base_url('tic/publicaciones?pub='.$id);

		/**** Carro de compras ****/
		//msg si se realiza alguna operacion
		$params['mod_compra'] = $this->showMsgsCompra();
		//obtiene acceso tic ilim
		$this->load->model('skin_model');
		$acc_ilim = $this->db->query("SELECT ticDiasFaltantesIlim(".$this->session->userdata('id_usuario').") AS acc_ilim");
		$acc_ilim = $acc_ilim->row();
		$params['acc_ilim'] = json_decode($acc_ilim->acc_ilim);
		//precios
		$precios = $this->db->query("SELECT id_precio, precio, tipo, descuento FROM precios ORDER BY id_precio ASC");
		foreach ($precios->result() as $key => $value) {
			if(strpos($value->tipo, 'ilim') !== false){
				$params['precios']['ilim'][$value->tipo] = $value;
			}else{
				$params['precios']['artics'][$value->tipo] = $value;
			}
		}
		$params['barra_right'] = $this->load->view('tic/general/barra_right', $params, true);
		/**** Carro de compras ****/


		//Filtros
		$sql = '';
		$order = ($this->input->get('forden') == 'asc'? 'ASC': 'DESC');
		$sql   .= ($this->input->get('fbuscar') != ''? " AND UPPER(a.titulo) LIKE '%".mb_strtoupper($this->input->get('fbuscar'), 'utf-8')."%'": '');
		$sql   .= ($this->input->get('fmateria') != ''? " AND a.materia LIKE '".$this->input->get('fmateria')."'": '');
		if($this->input->get('fecha1')!='' && $this->input->get('fecha2')!=''){
			$sql .= " AND Date(a.fecha_creacion) BETWEEN '".$this->input->get('fecha1')."' AND '".$this->input->get('fecha2')."'";
		}
		//consulta
		$params['bpp'] = $this->db->query("SELECT
				a.id_articulo, a.id_tipo, a.numero, a.titulo, a.materia, a.variables,
				a.imagen1, a.imagen2, a.imagen3, a.fecha_creacion, at.nombre,
				ticDiasFaltantes(".$this->session->userdata('id_usuario').", a.id_articulo ) AS acceso
			FROM articulos AS a INNER JOIN articulos_tipo AS at ON a.id_tipo = at.id_tipo
			WHERE a.status = 1 AND Date(a.fecha_creacion) <= Date(Now()) AND at.id_tipo = ".$id.$sql."
			ORDER BY numero ".$order)->result();

		// Obtiene el total de mensajes pendientes por responder
    $params['total_mgs'] = $this->skin_model->get_total_mensajes_pendientes();

    $this->load->library('my_facebook');
    $params['fb_app_id'] = $this->my_facebook->APP_ID;

    $params['username'] = $this->session->userdata('nombre');

		$this->load->view('tic/header', $params);
		$this->load->view('tic/general/menu', $params);
		$this->load->view('tic/publicaciones/publica', $params);
		$this->load->view('tic/footer');
	}


	public function get_publicacion_ajax()
	{
		$this->load->model('skin_model');

		$params['arti'] = $this->db->query("SELECT
				a.id_articulo, a.id_tipo, a.numero, a.materia, a.variables,
				a.titulo, a.descripcion, a.fecha_creacion, a.imagen1, a.imagen2, a.imagen3, at.nombre,
				ticDiasFaltantes(".$this->session->userdata('id_usuario').", a.id_articulo ) AS acceso
			FROM articulos AS a INNER JOIN articulos_tipo AS at ON a.id_tipo = at.id_tipo
			WHERE a.id_articulo = ".$this->input->get('art'))->row();
		$params['arti']->acceso = json_decode($params['arti']->acceso);
		$fecha = explode("-", $params['arti']->fecha_creacion);
		$params['arti']->fecha_creacion = String::mes($fecha[1])." ".$fecha[0];

		//registra la visita a algun articulo
		$this->skin_model->registraVisita($this->session->userdata('id_usuario'), $this->input->get('art'), $this->session->userdata('idunico') );

		$params['html'] = $this->load->view('tic/publicaciones/leermas', $params, true);

		echo json_encode($params);
	}



	/**
	 * Muestra mensajes cuando se realiza alguna accion
	 */
	private function showMsgsCompra($title='Compra')
	{
		$txt='';
		$icono='';
		switch($this->input->get('py')){
			case 'f': //queda pendiente el pago
				$txt = $this->load->view('tic/paquete/agregado', array('id_paquete' => $this->input->get('n')), true);
				$icono = 'success';
				break;
			case 't': //comprado en paypal
				//confirma la compra
				$this->load->model('skin_model');
				$this->skin_model->doPayment($this->input->get('n'));
				//modifico el paquete que quede activo
				$this->db->update('paquetes', array('status' => 1), "id_paquete IN(".$this->input->get('n').") ");
				if ($this->input->get('bc') == 't') {
					//elimina los productos del carrito
					$this->db->delete('paquetes_carrito', array('id_usuario' => $this->session->userdata('id_usuario')) );
				}

				$txt = $this->load->view('tic/paquete/payment', array('id_paquete' => $this->input->get('n')), true);
				$icono = 'success';
				break;
			case 'c': //cancelado de paypal
				$txt = $this->load->view('tic/paquete/cancel', array('id_paquete' => $this->input->get('n')), true);
				$icono = 'error';
				break;
		}

		return array(
				'title' => $title,
				'msg' => $txt,
				'ico' => $icono);
	}


}

